/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ronsicecream.pos.transaction.inputsaledetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.ronsicecream.pos.entities.PosTransactionInputSaleDetail;
//import com.ronsicecream.pos.master.icecream.IceCream;
//import com.ronsicecream.pos.master.product.Product;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author user
 */
public class SaleDetail extends Page {

    private Field saleDetailId;
    private Field saleDetailQuantity;
    private Field saleDetailPrice;
    private Field saleDetailDiscountPercent;
    private Field saleDetailDiscountNominal;
    private Field saleDetailTotal;
    private Field saleDetailSaleId;
    private Field saleDetailItemId;
    private Field saleDetailPriceOutletId;

    public SaleDetail() {
        super();

        setTheClass(PosTransactionInputSaleDetail.class);




        saleDetailId = new Field();
        saleDetailId.setName("saleDetailId");
        saleDetailId.setCaption(" id");
        //saleDetailId.setInfo("sale detail id");
        saleDetailId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        saleDetailId.setInputProperties(" size=\"20\"");
        getFields().add(saleDetailId);

        saleDetailQuantity = new Field();
        saleDetailQuantity.setName("saleDetailQuantity");
        saleDetailQuantity.setCaption("quantity");
        saleDetailQuantity.setRequired(true);
        saleDetailQuantity.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        saleDetailQuantity.setInputProperties(" size=\"20\"");
        getFields().add(saleDetailQuantity);

        saleDetailPrice = new Field();
        saleDetailPrice.setName("saleDetailPrice");
        saleDetailPrice.setCaption("price");
        saleDetailPrice.setRequired(true);
        saleDetailPrice.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        saleDetailPrice.setInputProperties(" size=\"20\"");
        getFields().add(saleDetailPrice);

        saleDetailDiscountPercent = new Field();
        saleDetailDiscountPercent.setName("saleDetailDiscountPercent");
        saleDetailDiscountPercent.setCaption("disc percent");
        saleDetailDiscountPercent.setDefaultValue(0);
        saleDetailDiscountPercent.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        saleDetailDiscountPercent.setInputProperties(" size=\"20\"");
        getFields().add(saleDetailDiscountPercent);

        saleDetailDiscountNominal = new Field();
        saleDetailDiscountNominal.setName("saleDetailDiscountNominal");
        saleDetailDiscountNominal.setCaption("disc nominal");
        saleDetailDiscountNominal.setDefaultValue(0);
        saleDetailDiscountNominal.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        saleDetailDiscountNominal.setInputProperties(" size=\"20\"");
        getFields().add(saleDetailDiscountNominal);

        saleDetailTotal = new Field();
        saleDetailTotal.setName("saleDetailTotal");
        saleDetailTotal.setCaption("total");
        //saleDetailTotal.setInfo("sale detail total");
        saleDetailTotal.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        saleDetailTotal.setInputProperties(" size=\"20\"");
        getFields().add(saleDetailTotal);

        saleDetailSaleId = new Field();
        saleDetailSaleId.setName("saleDetailSaleId");
        saleDetailSaleId.setCaption("input sale");
        //posTransactionInputSale.setInfo("pos transaction input sale");
        saleDetailSaleId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        saleDetailSaleId.setInputProperties(" size=\"20\"");
        getFields().add(saleDetailSaleId);

        saleDetailPriceOutletId = new Field();
        saleDetailPriceOutletId.setName("saleDetailPriceOutletId");
        saleDetailPriceOutletId.setCaption("price");
        saleDetailPriceOutletId.setRequired(true);
        saleDetailPriceOutletId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        saleDetailPriceOutletId.setInputProperties(" size=\"20\"");
        getFields().add(saleDetailPriceOutletId);

        saleDetailItemId = new Field();
        saleDetailItemId.setName("saleDetailItemId");
        saleDetailItemId.setCaption("item");
        saleDetailItemId.setSelectorCondition(" t.itemTypeId.typeId=5");
        saleDetailItemId.getSelectorAction().add(new String[]{"value", "saleDetailPrice", "itemPriceSell"});
        saleDetailItemId.getSelectorColumns().add(new String[]{"itemName", " Nama", "width=\"40%\""});
        saleDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"10%\" nowrap"});
        saleDetailItemId.getSelectorColumns().add(new String[]{"itemTypeId", " Type", "width=\"20%\""});
        saleDetailItemId.getSelectorColumns().add(new String[]{"itemMinimum", " Minimum", "width=\"10%\""});
        saleDetailItemId.getSelectorColumns().add(new String[]{"itemSatuanId", " Satuan", "width=\"10%\""});
        saleDetailItemId.getSelectorColumns().add(new String[]{"itemRasaId", " Rasa", "width=\"10%\""});
        saleDetailItemId.setSelectorOrder("t.itemName");
        saleDetailItemId.setRequired(true);
        saleDetailItemId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        saleDetailItemId.setInputProperties(" size=\"20\"");
        getFields().add(saleDetailItemId);


        fillFields();

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);

        getFieldsAdd().clear();
        getFieldsAdd().add(saleDetailItemId);
        getFieldsAdd().add(saleDetailQuantity);
//        getFieldsAdd().add(saleDetailPriceId);
//        getFieldsAdd().add(saleDetailPrice);
        getFieldsAdd().add(saleDetailDiscountPercent);
        getFieldsAdd().add(saleDetailDiscountNominal);

        getFieldsEdit().clear();
        getFieldsEdit().add(saleDetailItemId);
        getFieldsEdit().add(saleDetailQuantity);
//        getFieldsEdit().add(saleDetailPriceId);
//        getFieldsEdit().add(saleDetailPrice);
        getFieldsEdit().add(saleDetailDiscountPercent);
        getFieldsEdit().add(saleDetailDiscountNominal);

        getFieldsList().clear();
        getFieldsList().add(saleDetailItemId);
        getFieldsList().add(saleDetailQuantity);
        getFieldsList().add(saleDetailPrice);
        getFieldsList().add(saleDetailDiscountPercent);
        getFieldsList().add(saleDetailDiscountNominal);
        getFieldsList().add(saleDetailTotal);

//        setController(new SaleDetailController(this));
    }

//    class SaleDetailController extends Controller {
//
//        public SaleDetailController(Page page) {
//            super(page);
//        }
//
//        @Override
//        public void beforeInsertDetail(Object data) {
//            EntityManager em = getEntityManager();
//            PosMasterStatus statusGood = Status.getStatusGood(em);
//            System.out.println(statusGood.getStatusName());
//            PosTransactionInputSaleDetail saleDetail = (PosTransactionInputSaleDetail) data;
//            PosMasterProductOutletStock stock = IceCream.getStockIceCream(em, saleDetail.getSaleDetailIceCreamId(), getCurrentUnit(), statusGood, saleDetail.getSaleDetailSaleId().getSaleOutletId());
//            BigDecimal total = BigDecimal.ZERO;
//            BigDecimal quantity = saleDetail.getSaleDetailQuantity();
//            total = saleDetail.getSaleDetailPrice().multiply(quantity);
//            BigDecimal percent = BigDecimal.ZERO;
//            try {
//                percent = total.multiply(saleDetail.getSaleDetailDiscountPercent().divide(new BigDecimal(100)));
//            } catch (Exception e) {
//                Logger.getInstance().log(e);
//            }
//            total = total.subtract(percent);
//            try {
//                total = total.subtract(saleDetail.getSaleDetailDiscountNominal());
//            } catch (Exception e) {
//                Logger.getInstance().log(e);
//            }
//            saleDetail.setSaleDetailTotal(total);
//        }
//    }
    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        saleDetailItemId.setSelectorCondition("t.itemId IN (SELECT t1.itemStockItemId.itemId FROM PosMasterItemStock t1 WHERE t1.itemStockUnitId.unitId=" + getCurrentUnit().getUnitId() + " AND t1.itemStockValue > 0) ");

    }

//    @Override
//    public String validate(Object data) {
//        EntityManager em = getEntityManager();
//        PosMasterStatus statusGood = Status.getStatusGood(em);
//        PosTransactionInputSaleDetail saleDetail = (PosTransactionInputSaleDetail) data;
//        PosMasterProductOutletStock stock = IceCream.getStockIceCream(em, saleDetail.getSaleDetailIceCreamId(), getCurrentUnit(), statusGood, saleDetail.getSaleDetailSaleId().getSaleOutletId());
////        int tot = stock.getStockProductValue()- stock.getStockProductPrepare();
//        if (saleDetail.getSaleDetailQuantity().compareTo(stock.getProductStockValue()) > 0 || stock.getProductStockValue().intValue()==0) {
//            return saleDetail.getSaleDetailIceCreamId() + " " + Dictionary.getValue(this, "insufficient stock") + ", " + Dictionary.getValue(this, "current stock: ") + ": " + stock.getProductStockValue() ;
//        }
//        return null;
//    }
//    @Override
//    public StringBuffer process() {
//        if (getAction().equals(Action.TRANS_DETAIL_LIST)) {
//            Template template = new Template();
//            template.loadTemplate("/com/buanalintas/maika/pos/saledetail/trans-list.html");
//            return PageTransactionDetailList.process(this, template);
//        } else {
//            return super.process();
//        }
//    }
//
//    @Override
//    public String doAction() {
//        String barcode = Utility.getPostParameterEmpty(this, "barcode");
//        if (!"".equals(barcode)) {
//            HttpSession session = getRequest().getSession();
//            try {
//                EntityManager em = getEntityManager();
//                PosMasterIceCream product = (PosMasterIceCream) em.createQuery("SELECT t FROM PosMasterProduct t WHERE t.productBarcode='" + barcode + "'").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//                List<PosTransactionInputSaleDetail> list = ((List) session.getAttribute("transactions"));
//                boolean exists = false;
//                for (PosTransactionInputSaleDetail sd : list) {
//                    if (sd.getSaleDetailIceCreamId().equals(product) || sd.getSaleDetailQuantity().intValue() != 0) {
//                        exists = true;
//                        break;
//                    }
//                }
//                if (!exists) {
//                    PosTransactionInputSaleDetail detail = new PosTransactionInputSaleDetail();
//                    detail.setSaleDetailIceCreamId(product);
//                    detail.setSaleDetailPrice(product.getProductPriceSell());
//                    detail.setSaleDetailQuantity(BigDecimal.ONE);
//                    detail.setSaleDetailTotal(detail.getSaleDetailPrice().multiply(new BigDecimal(detail.getSaleDetailQuantity())));
//                    list.add(detail);
//                }
//            } catch (Exception e) {
//                Logger.getInstance().log(e);
//            }
//        }
//        return super.doAction();
//    }
//    class SaleDetailController extends Controller {
//
//        public SaleDetailController(Page page) {
//            super(page);
//        }
//
//        @Override
//        public void beforeInsertDetail(Object data) {
//            EntityManager em = getEntityManager();
//            PosMasterStatus statusGood = Status.getStatusGood(em);
//            System.out.println(statusGood.getStatusName());
//            PosTransactionInputSaleDetail saleDetail = (PosTransactionInputSaleDetail) data;
//            
//            PosMasterUnit posunit = (PosMasterUnit) em.createQuery("SELECT t FROM PosMasterUnit t"
//                            + " WHERE t.unitUnitTypeId.unitTypeId=4 "
//                            + " AND t.unitUnitId.unitId="+getCurrentUnit().getUnitId())
////                            + " AND t.unitRegionalId.regionalId="+sale.getSaleRegionalId().getRegionalId())
//                            .setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//                    System.out.println("unittt :" + posunit + " unit name: " + posunit.getUnitName() );
//                    
//            
//            PosMasterPriceOutlet price = (PosMasterPriceOutlet) em.createQuery("SELECT t FROM PosMasterPriceOutlet t"
//                        + " WHERE t.pricePosUnitId.unitId=" + posunit.getUnitId()
//                        + " AND t.priceItemId.itemId=" + saleDetail.getSaleDetailItemId().getItemId()
//                        )
//                        .setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//            saleDetail.setSaleDetailPrice(price.getPricePrice());
//
////            PosMasterStockProduct stock = Product.getStock(em, saleDetail.getSaleDetailProductId(), getCurrentUnit(), statusGood, saleDetail.getSaleDetailProductId().getProductTypeId());
//            BigDecimal total = BigDecimal.ZERO;
////            BigDecimal quantity = new BigDecimal(saleDetail.getSaleDetailQuantity());
//            total = saleDetail.getSaleDetailPrice().multiply(saleDetail.getSaleDetailQuantity());
//            BigDecimal percent = BigDecimal.ZERO;
//            try {
//                percent = total.multiply(saleDetail.getSaleDetailDiscountPercent().divide(new BigDecimal(100)));
//            } catch (Exception e) {
//                Logger.getInstance().log(e);
//            }
//            total = total.subtract(percent);
//            try {
//                total = total.subtract(saleDetail.getSaleDetailDiscountNominal());
//            } catch (Exception e) {
//                Logger.getInstance().log(e);
//            }
//            saleDetail.setSaleDetailTotal(total);
//        }
//    }
}
